<script type="text/javascript">
    var wechatUtils = (function($, wechatUtils) {
        var cfg_def = {
            /*code验证服务*/
            authByCode: "/api/gzd/ignore/wechat/authByCode"
            /*验证错误跳转的模块名称*/
            ,pageName: "beforLogin"
            /*验证错误跳转的url key*/
            ,urlName: "login"
            /*验证成功回调*/
            ,authSuccess: function(){}
        }
        wechatUtils.auth = function(cfg){
            $.extend(wechatUtils, cfg_def, cfg);
            var href = window.location.href;
            var index = href.indexOf("?");
            if(index == -1){
                index = href.length;
            }
            href = href.substring(0, index);
            var params = $.QuickUrlUtils.getRequest();
            var code = params["code"];
            if(!StringUtils.isEmpty(code)) {
                var path = window.location.pathname;
                history.replaceState({}, "", path);
            }
            if(!StringUtils.isEmpty(code)) {
                $.QuickRemote.AjaxJson({
                    funcName: wechatUtils.authByCode
                    ,params: {code: code}
                    ,callback: function (result) {
                        var auth = result['auth'];
                        if ("0" == auth) {
                            var key = result['key'];
                            $.pageSwitch.goToUrl(wechatUtils.pageName, wechatUtils.urlName, 'key=' + key + "&redirect_uri=" + encodeURI(href));
                        }else {
                            refreshUserInfo();
                        }
                    }
                    ,errorback: function(){
                        $.pageSwitch.goToUrl(wechatUtils.pageName, wechatUtils.urlName, "redirect_uri=" + encodeURI(href))
                    }
                });
            }
            else{
                refreshUserInfo();
            }
        }
        return wechatUtils;

        function refreshUserInfo(){
            $.JkcsUserInfo.getUserInfoGzd({
                refresh: true
                ,callback: function(userInfo){
                    /*之前绑定过则直接初始化页面*/
                    wechatUtils.authSuccess();
                }
            });
        }

    })(jQuery, {});
    var wechatLogout = (function($, wechatLogout) {
        var uri_service_logoutWechat = '/api/gzd/wechat/logout';

        wechatLogout.logout = function(){
            logout();
        }

        return wechatLogout;

        function logout(){
            $.QuickRemote.AjaxJson({
                funcName: uri_service_logoutWechat
                ,callback: function(){
                    clearCookis();
                    var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx19000fcda6138605&redirect_uri=http%3a%2f%2fmhealth.jynstudio.com%2fhtml%2fgzd%2fnavigation%2fpersonal.html&response_type=code&scope=snsapi_base&state=jyfw#wechat_redirect";
                    $.QuickUrlUtils.redirectJSBridge({
                        url: url
                    });
                }
            });
        }
    })(jQuery, {});
</script>
